<template>
  <div
    class="login">
    <vue-particles
      color="#40356a"
      :particleOpacity="0.7"
      :particlesNumber="60"
      shapeType="circle"
      :particleSize="2"
      linesColor="#dedede"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="3"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
    >

    </vue-particles>
    <div class="login-box">
      <div class="image-wrap">
        <img src="../assets/logo.png">
      </div>
      <el-form
        :model="loginForm"
        ref="loginForm"
        :rules="rules"
        :status-icon="true"
      >
        <el-form-item prop="mobile">
          <el-input v-model="loginForm.mobile">
            <template
              slot="prepend"
              width="150"
            >
              用户名
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
          >
            <template slot="prepend">密&nbsp;&nbsp;&nbsp;码</template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            style="width:100%;"
            @click="check('loginForm')"
          >登陆</el-button>
        </el-form-item>
        <el-form-item style="text-align:right">
          <router-link to="/signup">注册</router-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Login',
    data() {
      return {
        loginForm: {
          mobile: '13166315160',
          password: '123123'
        },
        rules: {
          mobile: [{
            required: true,
            message: '请输入用户名',
            trigger: 'change'
          }],
          password: [{
            required: true,
            message: '请输入密码',
            trigger: 'change'
          }],
        }

      }
    },
    methods: {
      check(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$http.post('/user/login/', this.loginForm).then(
              response => {
                this.$store.commit('setUser', response.data.user)
                this.$store.commit('setToken', response.data.token)
                this.$router.push('/home')
              }
            )
          } else {
            console.log('error submit!!');
            return false;
          }
        })
      }
    }
  }

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style
  lang=scss
  scoped
>
  .login {
    height: 100%;
    .login-box {
      width: 50%;
      max-width: 600px;
      position: absolute;
      top: 50%;
      left: 50%;
      z-index: 10;
      transform: translate(-50%, -50%);
      box-sizing: border-box;
      /* box-shadow: 0 0 25px #cac6c6;
      border: 1px solid #eaeaea; */
      padding: 35px 35px 15px;
      .image-wrap{
        text-align: center;
        padding-bottom: 30px;
      }
    }
  }

</style>
